package com.jeeplus.test.jlnu.studentStatusInfo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AdmissionInfo;
import com.jeeplus.test.jlnu.admissionInfo.domain.entity.AllInfo;
import com.jeeplus.test.jlnu.archivalInfo.domain.entity.ArchParmerter;
import com.jeeplus.test.jlnu.archivalInfo.domain.vo.ArchivalInfoReqVo;
import com.jeeplus.test.jlnu.archivalInfo.service.ArchivalInfoService;
import com.jeeplus.test.jlnu.studentStatusInfo.domain.entity.StudentStatusInfo;
import com.jeeplus.test.jlnu.studentStatusInfo.domain.vo.StudentStatusInfoReqVo;
import com.jeeplus.test.jlnu.studentStatusInfo.service.StudentStatusInfoService;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "学籍信息")
@RestController
@Slf4j
@RequestMapping("/studentStatusInfo")
public class StudentStatusInfoController {

    @Autowired
    private StudentStatusInfoService studentStatusInfoService;

    @Autowired
    private ArchivalInfoService archivalInfoService;

    @ApiOperation("学籍信息分页查询")
    @GetMapping("/page")
    public Page<StudentStatusInfo> getStudentStatusInfoPage(ArchParmerter studentStatusInfoReqVo, @RequestParam(defaultValue = "1") int pageNum,
                                                  @RequestParam(defaultValue = "10") int pageSize) {
        Page<StudentStatusInfo> page = new Page<>(pageNum, pageSize);
        Page<StudentStatusInfo> pageResult = studentStatusInfoService.getStudentStatusInfoPage(studentStatusInfoReqVo, page);
        return pageResult;
    }

    @ApiOperation("根据学号修改学籍信息")
    @PostMapping("/updateStudentStatus")
    public R updateStudentStatus(@RequestBody StudentStatusInfoReqVo studentStatusInfoReqVo){
        int i = studentStatusInfoService.updateStudentStatus(studentStatusInfoReqVo);
        if(i>0){
            return R.suc("修改成功");
        }else {
            return R.error();
        }
    }
    @ApiOperation("学籍信息新增")
    @PostMapping("/addStudentStatusInfo")
    public  R addStudentStatusInfo (@RequestBody List<StudentStatusInfoReqVo> studentStatusInfos){

        for (StudentStatusInfoReqVo studentStatusInfo : studentStatusInfos) {
            //验证当前新增的学号是不是在主表中出现 如果出现了 那么只需要更新当前表的内容即可 不需要新增 没有才新增并且在主表插入学号和其他数据和其他表中插入id
            int i = archivalInfoService.checkStuId(studentStatusInfo.getStudentId());
            if(i>0){
                studentStatusInfoService.updateStudentStatus(studentStatusInfo);
            }else {
                studentStatusInfoService.addStudentInfo(studentStatusInfo);
                ArchivalInfoReqVo admissionInfo = new ArchivalInfoReqVo();
                BeanUtils.copyProperties(studentStatusInfo, admissionInfo);
                archivalInfoService.addAddmissionStuId(admissionInfo);
            }
        }
        return R.suc();
    }

}
